package com.hznc.wcs.controller.bpm;

import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.entity.BaseLoginUser;
import com.diboot.iam.util.IamSecurityUtils;
import com.hznc.wcs.service.bpm.BpmProcessInstanceService;
import com.hznc.wcs.vo.bpm.BpmProcessInstanceCreateReqVO;
import com.hznc.wcs.vo.bpm.BpmProcessInstanceReqVO;
import com.hznc.wcs.vo.bpm.BpmProcessInstanceRespVO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description : 流程实例接口
 * @Date : 2024/6/20
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@Slf4j
@RestController
@BindPermission(name = "流程实例",code = "BpmInstance")
@RequestMapping("/bpm-instance")
public class BpmInstanceController {
    
    @Resource
    private BpmProcessInstanceService bpmProcessInstanceService;

    /**
     * 查询我发起的流程
     * @param reqVO
     * @param pagination
     * @return
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST,code = OperationCons.CODE_READ)
    @GetMapping("/getMyInstancePage")
    public JsonResult<?> getMyInstancePage(@Valid BpmProcessInstanceReqVO reqVO, Pagination pagination){
        BaseLoginUser currentUser = IamSecurityUtils.getCurrentUser();
        reqVO.setStartUserId(currentUser.getId());
        List<BpmProcessInstanceRespVO> instanceList = bpmProcessInstanceService.getBpmProcessInstanceList(reqVO, pagination);
        return JsonResult.OK(instanceList).bindPagination(pagination);
    }

    /**
     * 查询已发起的流程实例
     * @param reqVO
     * @param pagination
     * @return
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST,code = OperationCons.CODE_READ)
    @GetMapping("/getInstancePage")
    public JsonResult<?> getInstancePage(@Valid BpmProcessInstanceReqVO reqVO, Pagination pagination){
        List<BpmProcessInstanceRespVO> instanceList = bpmProcessInstanceService.getBpmProcessInstanceList(reqVO, pagination);
        return JsonResult.OK(instanceList).bindPagination(pagination);
    }

    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL,code = OperationCons.CODE_READ)
    @GetMapping("/getInstanceInfo/{id}")
    public JsonResult<?> getInstanceInfo(@PathVariable("id") String id){
        BpmProcessInstanceRespVO bpmProcessInstanceRespVO = bpmProcessInstanceService.getBpmProcessInstance(id);
        return JsonResult.OK(bpmProcessInstanceRespVO);
    }
    
    

    /**
     * 创建流程实例（发起流程）
     * @param createReqVO
     * @return
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/createInstance")
    public JsonResult<String> createProcessInstance(@Valid @RequestBody BpmProcessInstanceCreateReqVO createReqVO) {
        String processInstanceId = bpmProcessInstanceService.createProcessInstance(createReqVO);
        return JsonResult.OK(processInstanceId);
    }

}
